
#include <BGridLayout>
#include <BHBoxLayout>
#include <BVBoxLayout>
#include <BLabel>
#include <BSpring>
#include "member_BColorDialog.h"

using namespace BWE;

member_BColorDialog::member_BColorDialog(BColorDialog* colorDialog)
{
	boss = colorDialog;
	color.a() = 255;

	BHBoxLayout* spacerLayout = new BHBoxLayout();
	spacerLayout->setSpacing(2);
	{
		drawSpacer = new BSpacer();
		drawSpacer->setMinSize(BSize(200, 200));
		graySpacer = new BSpacer(16, 16);
		graySpacer->setSizePolicy(BSizePolicy(Policy_Fixed, Policy_Dynamic));
		spacerLayout->addSpacer(drawSpacer, 100);
		spacerLayout->addSpacer(graySpacer);
	}

	BGridLayout* gridLayout = new BGridLayout();
	gridLayout->setSpacing(2);
	{
		BLabel* redLabel = new BLabel("R:");
		redSlider = new BSlider(0, 255);
		redSpinBox = new BSpinBox(0, 255);
		redSpinBox->setButtonShown(true);

		BLabel* greenLabel = new BLabel("G:");
		greenSlider = new BSlider(0, 255);
		greenSpinBox = new BSpinBox(0, 255);
		greenSpinBox->setButtonShown(true);

		BLabel* blueLabel = new BLabel("B:");
		blueSlider = new BSlider(0, 255);
		blueSpinBox = new BSpinBox(0, 255);
		blueSpinBox->setButtonShown(true);

		BLabel*	alphaLabel = new BLabel("A:");
		alphaSlider = new BSlider(0, 255);
		alphaSpinBox = new BSpinBox(0, 255);
		alphaSpinBox->setButtonShown(true);

		gridLayout->setWidget(0, 0, redLabel);
		gridLayout->setWidget(0, 1, redSlider);
		gridLayout->setWidget(0, 2, redSpinBox);
		gridLayout->setWidget(1, 0, greenLabel);
		gridLayout->setWidget(1, 1, greenSlider);
		gridLayout->setWidget(1, 2, greenSpinBox);
		gridLayout->setWidget(2, 0, blueLabel);
		gridLayout->setWidget(2, 1, blueSlider);
		gridLayout->setWidget(2, 2, blueSpinBox);
		gridLayout->setWidget(3, 0, alphaLabel);
		gridLayout->setWidget(3, 1, alphaSlider);
		gridLayout->setWidget(3, 2, alphaSpinBox);
		gridLayout->setColumnStretch(1, 10);
	}

	colorSpacer = new BSpacer(100, 100, Policy_Fixed, Policy_Fixed);

	BHBoxLayout* hlayout = new BHBoxLayout();
	hlayout->addLayout(gridLayout);
	hlayout->addSpacer(colorSpacer);

	BVBoxLayout* layout = new BVBoxLayout(boss);
	layout->setMargin(5);
	layout->setSpacing(5);
	layout->addLayout(spacerLayout);
	layout->addLayout(hlayout, 0);

	BHBoxLayout* buttonLayout = new BHBoxLayout();
	{
		okButton = new BButton("Ok");
		cancleButton = new BButton("Cancel");

		buttonLayout->setSpacing(3);
		buttonLayout->addSpring();
		buttonLayout->addWidget(okButton);
		buttonLayout->addWidget(cancleButton);
	}
	boss->setLayout(buttonLayout, Part_Bottom);

	connect(redSlider, Signal_Sliding, &member_BColorDialog::slotColorSlided);
	connect(redSpinBox, Signal_ValueEdited, &member_BColorDialog::slotColorChanged);
	connect(greenSlider, Signal_Sliding, &member_BColorDialog::slotColorSlided);
	connect(greenSpinBox, Signal_ValueEdited, &member_BColorDialog::slotColorChanged);
	connect(blueSlider, Signal_Sliding, &member_BColorDialog::slotColorSlided);
	connect(blueSpinBox, Signal_ValueEdited, &member_BColorDialog::slotColorChanged);
	connect(alphaSlider, Signal_Sliding, &member_BColorDialog::slotColorSlided);
	connect(alphaSpinBox, Signal_ValueEdited, &member_BColorDialog::slotColorChanged);
	connect(okButton, Signal_Clicked, &member_BColorDialog::slotOkButtonClicked);
	connect(cancleButton, Signal_Clicked, &member_BColorDialog::slotCancleButtonClicked);

}
member_BColorDialog::~member_BColorDialog()
{

}

void member_BColorDialog::freshGraph()
{
	BRect drawRect = drawSpacer->rect();
	BRect grayRect = graySpacer->rect();

	int cx = (int)(drawRect.x() + drawRect.width() * greenSlider->ratio());
	int cy = (int)(drawRect.y() + drawRect.height() * redSlider->ratio());
	int size = grayRect.width() / 2;
	colorRect.set(cx - size / 2, cy - size / 2, size, size);

	BByte max = bMax(color.r(), bMax(color.g(), color.b()));
	int y = (int)(grayRect.bottom() - max / 255.0 * grayRect.height());
	grayHandle.set(grayRect.left() + size / 2, y - size / 2, size, size);
}

void member_BColorDialog::freshWidgets()
{
	redSlider->setValue(color.r());
	redSpinBox->setValue(color.r());
	greenSlider->setValue(color.g());
	greenSpinBox->setValue(color.g());
	blueSlider->setValue(color.b());
	blueSpinBox->setValue(color.b());
	alphaSlider->setValue(color.a());
	alphaSpinBox->setValue(color.a());
}

void member_BColorDialog::slotChoose(BObject* object, const BValue& value)
{
	BButton* button = dynamic_cast<BButton*>(object);
	if (button)
	{
		color = button->color();
	}
}
void member_BColorDialog::slotColorSlided()
{
	int r = redSlider->value();
	int g = greenSlider->value();
	int b = blueSlider->value();
	int a = alphaSlider->value();
	boss->setColor(BColor(r, g, b, a));
}
void member_BColorDialog::slotColorChanged()
{
	int r = redSpinBox->value();
	int g = greenSpinBox->value();
	int b = blueSpinBox->value();
	int a = alphaSpinBox->value();
	boss->setColor(BColor(r, g, b, a));
}

void member_BColorDialog::slotOkButtonClicked()
{
	boss->close(1);
}
void member_BColorDialog::slotCancleButtonClicked()
{
	boss->hide();
}

